/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

/**
 * Log.java
 * This class holds connections, if connection is broken then it recreates it
 */

import java.sql.*;
import java.io.*;
import java.util.*;

import blueprint4j.utils.*;

public class DBLog extends Entity {

	static String VERSION = "$Id: DBLog.java,v 1.4 2004/09/09 11:07:54 jaspervdb Exp $";

	public static final String TABLE_NAME = "d_logs";
	public TableName table_name = new TableName(TABLE_NAME,this);
	public FieldUnique id = new FieldUnique("id", 0, "The Record Identifier", this);
	public FieldString log_level = new FieldString("log_level",0,"the level of this log",10,this);
	public FieldRename rename_log_level = new FieldRename("level",log_level,this);
	public FieldString description = new FieldString("description",0,"the description",250,this);
	public FieldText detail = new FieldText("detail",0,"the detail",this);
	public FieldThreadId thread_id = new FieldThreadId("thread_id",0,"The thread id this was created",this);
	public FieldDateTime logtime = new FieldDateTime("logtime",0,"The time of this log",this);

	private Field fields[] = new Field[]{log_level,description,detail,thread_id,logtime};

	private Maintenance maintenance = new Maintenance(this,new Maintenance.Action[]{new Maintenance.ActionTrim(500)},60*1000*60*5);
	private Index index_level = new Index("index_level",new Field[]{log_level},this);


	public DBLog() {}

	public DBLog(DBConnection connection) throws DataException,SQLException {super(connection);}
	
	public DBLog(DBConnection connection,ResultSet result_set) throws DataException,SQLException {
		super(connection);
		load(result_set);
	}

	public Entity getNewInstance() {
		return new DBLog();
	}

	public String getVersionString() {
		return VERSION;
	}

	public String toString() {
		return description.get();
	}

	public LogDataUnit transfromToLogDataUnit() {
		if (log_level.get().equals(Logging.TRACE.toString())) {
			return new LogDataUnit(Logging.TRACE,description.get(),detail.get(),logtime.get());
		}
		if (log_level.get().equals(Logging.DEBUG.toString())) {
			return new LogDataUnit(Logging.DEBUG,description.get(),detail.get(),logtime.get());
		}
		if (log_level.get().equals(Logging.CRITICAL.toString())) {
			return new LogDataUnit(Logging.CRITICAL,description.get(),detail.get(),logtime.get());
		}
		return null;
	}

	public static DBLog findLogs(DBConnection connection,Integer tid,java.util.Date from_date,java.util.Date to_date,Logging.Level levels[])
	throws DataException,SQLException {
		String sql = "select * from "  + TABLE_NAME + " where ";
		if (tid != null) {
			sql += "tid = " + tid + " and ";
		}
		if (from_date != null && to_date != null) {
			sql += "logtime between " + connection.getDataBaseUtils().convertDateTimeToSQLString(from_date) +
			       " and " + connection.getDataBaseUtils().convertDateTimeToSQLString(to_date) + " and ";
		} else if (from_date != null) {
			sql += "logtime between " + connection.getDataBaseUtils().convertDateTimeToSQLString(from_date) +
			       " and " + connection.getDataBaseUtils().convertDateTimeToSQLString(new java.util.Date()) + " and ";
		}
		if (levels != null && levels.length > 0) {
			sql += " (";
			for (int i = 0;levels != null && i < levels.length;i++) {
				sql += "level = '"+levels[i]+"' or ";
			}
			sql += ") and ";
		}
		if (sql.length()==("select * from " + TABLE_NAME + " where ").length()) {
			sql = sql.substring(0,("select * from " + TABLE_NAME).length());
		} else {
			sql = sql.substring(0,sql.length() - " and ".length());
		}
		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery(sql);
		if (result.next()) {
			return new DBLog(connection,result);
		} else {
			statement.close();
		}
		return null;
	}

}
